package com.dy.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dy.cloud.pojo.Entity.PaymentEntity;
import com.dy.cloud.service.PaymentService;
import com.dy.cloud.util.ApiResousUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/9/2 17:15
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private String port;
    /**
     * 全查询
     * @return ApiResousUtil
     **/
    @GetMapping("/list")
    public ApiResousUtil<List> list(){
        List<PaymentEntity> list = paymentService.list(null);
        return ApiResousUtil.ok(list+"port"+port);
    }
    /**
     * 添加
     * @param  payment
     * @return ApiResousUtil
     **/
    @PostMapping("/save")
    public ApiResousUtil save(@RequestBody PaymentEntity payment){
        paymentService.save(payment);
        return ApiResousUtil.ok("port"+port);
    }
    /**
     * 删除
     * @param id
     * @return
     **/
    @GetMapping("/deleteById/{id}")
    public ApiResousUtil<String> deleteById(@PathVariable("id") Long id){
        System.out.println("id = " + id);
        paymentService.remove(new LambdaQueryWrapper<PaymentEntity>().eq(PaymentEntity::getId,id));
        return ApiResousUtil.ok("port"+port);
    }
    /**
     * 根据id查询
     * @param id
     * @return
     **/
    @GetMapping("/selectById/{id}")
    public ApiResousUtil<String> selectById(@PathVariable("id") Long id){
        PaymentEntity one = paymentService.getOne(new LambdaQueryWrapper<PaymentEntity>().eq(PaymentEntity::getId, id));
        return ApiResousUtil.ok(one+"port"+port);

    }
    /**
     * 修改
     * @param payment
     * @return
     **/
    @PostMapping("/updateById")
    public ApiResousUtil<String> updateById(@RequestBody PaymentEntity payment){
        PaymentEntity one = paymentService.getOne(new LambdaQueryWrapper<PaymentEntity>().eq(PaymentEntity::getId, payment.getId()));
        BeanUtils.copyProperties(payment,one);
        paymentService.update(one,new LambdaQueryWrapper<PaymentEntity>().eq(PaymentEntity::getId,one.getId()));
        return ApiResousUtil.ok("port"+port);
    }
}
